const setStatus = require("../common/setStatus");
const { invoke, query } = require('../tools/index.js');

const DEALER = ("dealer").toUpperCase();
const DEALERDEAL = ("dealerDeal").toUpperCase();

const queryOptions = {
    username: "dealer22",
    chaincodeId: "dealer",
    fcn: null,
    args: [],
    host: '106.12.220.29',
    org: '2'
};

const invokeOptions = {
    host: "106.12.220.29",
    orderHost:"139.199.191.238",
    org:"2",
    username: "dealer22",
    chaincodeId: "dealer",
    channelId: "mychannel",
    fcn: null,
    args: [],
}

function setQuery(fcn, args) {
    return Object.assign(queryOptions, {fcn, args});
}

function setInvoke(fcn, args) {
    return Object.assign(invokeOptions, {fcn, args});
}

function getKeyLength(obj) {
    let length = 0
    for (let key in obj) {
        if(obj[key]==="") return 0;
        length ++;
    }
    return length
}

module.exports = {
    getQuery:async (ctx) => {
        const {QueryId} = ctx.request.query;
        if (!QueryId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [QueryId]));
        setStatus(ctx, 200,1,data);
    },
    // Dealer
    getDealer: async (ctx) => {
        const {DealerId} = ctx.request.query;
        if (!DealerId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [DealerId]))
        setStatus(ctx, 200,1,data);
    },
    getDealerAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["dealer"]))
        setStatus(ctx, 200,1,data);
    },
    createDealer:  async (ctx) => {
        const {Name ,Address} = ctx.request.body;
        if (!Name || !Address) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const DealerId = DEALER + new Date().getTime();
        const args = [DealerId, Name, Address];
        await invoke(setInvoke("create", ["dealer", ...args]));
        setStatus(ctx, 200,1,"");
    },
    changeDealer: async (ctx) => {
        const {DealerId, Name ,Address} = ctx.request.body;
        if(!DealerId||!Name||!Address) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [DealerId, Name, Address];
        await invoke(setInvoke("change", ["dealer",...args]));
        setStatus(ctx, 200,1,"");
    },
    delDealer: async (ctx) => {
        const {DealerId} = ctx.request.body;
        if(!DealerId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [DealerId]));
        setStatus(ctx, 200,1,"");
    },

    // dealerDeal
    getDealerDeal: async (ctx) => {
        const {DealerDealId} = ctx.request.query;
        if (!DealerDealId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [ DealerDealId]))
        setStatus(ctx, 200,1,data);
    },
    getDealerDealAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["dealerDeal"]))
        setStatus(ctx, 200,1,data);
    },
    createDealerDeal:  async (ctx) => {
        const {DealerId,SellerId,AntisepticTreatment,StartingTime,EndingTime,SalePrice,SalePriceUnit,Amount,Address} = ctx.request.body;
        if (!DealerId || !SellerId||!AntisepticTreatment||!StartingTime||!EndingTime||!SalePrice||!SalePriceUnit||!Amount||!Address) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const DealerDealId = DEALERDEAL + new Date().getTime();
        const args = [DealerDealId,DealerId,SellerId,AntisepticTreatment,StartingTime,EndingTime,SalePrice,SalePriceUnit,Amount,Address];
        await invoke(setInvoke("create", ["dealerDeal", ...args]));
        setStatus(ctx, 200,1,"");
    },
    changeDealerDeal: async (ctx) => {
        const {DealerDealId, DealerId,SellerId,AntisepticTreatment,StartingTime,EndingTime,SalePrice,SalePriceUnit,Amount,Address} = ctx.request.body;
        if (!DealerDealId||!DealerId || !SellerId||!AntisepticTreatment||!StartingTime||!EndingTime||!SalePrice||!SalePriceUnit||!Amount||!Address) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [DealerDealId, DealerId,SellerId,AntisepticTreatment,StartingTime,EndingTime,SalePrice,SalePriceUnit,Amount,Address];
        await invoke(setInvoke("change", ["dealerDeal", ...args]));
        setStatus(ctx, 200,1,"");
    },
    delDealerDeal: async (ctx) => {
        const {DealerDealId} = ctx.request.body;
        if(!DealerDealId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [DealerDealId]));
        setStatus(ctx, 200,1,"");
    },
};